﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Ttifa.Infrastructure.Utils;
using Ttifa.WebApiBase;

namespace Ttifa.WebApi.Controllers
{
    public class CreditCardController : ApiController
    {
        /// <summary>
        /// 站点id
        /// </summary>
        private static string SiteId = "974483935490736129";
        /// <summary>
        /// 用户id
        /// </summary>
        private static string UId = "979182844616114177";
        /// <summary>
        /// 接口站点地址
        /// </summary>
        private static string Host = "https://api.51ley.com/apis";

        [HttpGet]
        public ApiResult Bank()
        {
            return ApiRequest("/api/bank", new
            {
                stationid = SiteId
            });
        }

        [HttpGet]
        public ApiResult Topic()
        {
            return ApiRequest("/api/bank/topic", new
            {
                stationid = SiteId
            });
        }

        [HttpGet]
        public ApiResult Tag()
        {
            return ApiRequest("/api/bank/tag", new
            {
                stationid = SiteId
            });
        }

        [HttpPost]
        public ApiResult Card([FromBody]CardFilter filter)
        {
            var param = new Dictionary<string, object>();
            param.Add("stationid", SiteId);
            if (!string.IsNullOrEmpty(filter.bankIds))
                param.Add("bankIds", filter.bankIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
            if (!string.IsNullOrEmpty(filter.topicIds))
                param.Add("topicIds", filter.bankIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
            if (!string.IsNullOrEmpty(filter.tagIds))
                param.Add("tagIds", filter.bankIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));

            return ApiRequest("/api/bank/card", param, Infrastructure.Utils.HttpMethod.POST);
        }

        [HttpGet]
        public ApiResult CardInfo(long id)
        {
            return ApiRequest($"/api/bank/card/{id}", new
            {
                stationid = SiteId,
                id = id
            });
        }

        /// <summary>
        /// 申卡记录
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="mobile">手机号</param>
        /// <param name="idcard">身份证号</param>
        /// <param name="stationChannelId">站点通道id</param>
        /// <param name="stationCardChannelId">站点卡种通道id</param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult CardAccess([FromBody]CreditCardRecord record)
        {
            record.FlowNo = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            return new ApiResult(data: record);
            /*
            var result = ApiRequest($"/api/station/stationCardAccessRecords/save/{UId}", new
            {
                stationid = SiteId,
                userId = UId,
                name = record.Name,
                mobile = record.Mobile,
                idCard = record.Idcard,
                clientNo = DateTime.Now.ToString("yyyyMMddHHmmssffff"),
                callbackUrl = "",
                stationChannelId = record.StationChannelId,
                stationBankCardChannelId = record.StationCardChannelId
            }, Infrastructure.Utils.HttpMethod.POST);

            return result;
            */
        }

        [HttpPost]
        public ApiResult Callback(string clientNo, string tradeNo, string message, string callbackType)
        {
            return new ApiResult(data: callbackType);
        }

        private ApiResult ApiRequest(string api, object postData,
            Infrastructure.Utils.HttpMethod method = Infrastructure.Utils.HttpMethod.GET)
        {
            var url = Host + api;

            var resp = CommunicationHelper.Http(url, "application/json", JsonConvert.SerializeObject(postData), method);

            var result = JsonConvert.DeserializeObject<JObject>(resp);

            return new ApiResult(result["status"].ToString() == "200" ? ApiStatus.OK : ApiStatus.Fail,
                result["message"].ToString(), result["result"]);
        }
    }

    #region model
    public class CardFilter
    {
        public string bankIds { get; set; }
        public string topicIds { get; set; }
        public string tagIds { get; set; }
    }

    public class CreditCardRecord
    {
        public string FlowNo { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public string Idcard { get; set; }
        public string StationChannelId { get; set; }
        public string StationCardChannelId { get; set; }
        public int State { get; set; }
        public System.DateTime LogTime { get; set; }
        public Nullable<System.DateTime> CallbackTime { get; set; }
        public string Remark { get; set; }
    }
    #endregion
}
